package com.xoa.service.workflow.flowRule.impl;

import com.xoa.dao.workflow.FlowRuleMapper;
import com.xoa.model.users.Users;
import com.xoa.model.workflow.FlowRule;
import com.xoa.service.workflow.flowRule.FlowRuleService;
import com.xoa.util.CookiesUtil;
import com.xoa.util.ToJson;
import com.xoa.util.common.session.SessionUtils;
import com.xoa.util.page.PageParams;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by gsb on 2017/8/21.
 */
@Service
public class FlowRuleServiceImpl implements FlowRuleService{

    @Resource
    private  FlowRuleMapper flowRuleMapper;
    /**
     * 创建作者:   季佳伟
     * 创建日期:   2017/8/21 16:52
     * 方法介绍:   查询工作委托的所有数据
     * 参数说明:
     * @return
     */
    @Override
    public ToJson<FlowRule> queryFlowRule(Integer page,
                                          Integer pageSize, boolean useFlag){
        ToJson<FlowRule> json = new ToJson<FlowRule>();
        try{
            Map<String, Object> maps = new HashMap<String, Object>();
            PageParams pageParams = new PageParams();
            pageParams.setPage(page);
            pageParams.setPageSize(pageSize);
            pageParams.setUseFlag(useFlag);
            maps.put("page", pageParams);
            List<FlowRule> flowRules = flowRuleMapper.queryFlowRule(maps);
            //循环添加名字
            for (FlowRule flowRule:flowRules){
                flowRule.setUserIdName(flowRuleMapper.selectUserNameByUserId(flowRule.getUserId()));
                if (flowRule.getFlowId()==0){
                    flowRule.setFlowName("全部流程");
                }else {
                    flowRule.setFlowName(flowRuleMapper.selectFlowNameByFlowId(flowRule.getFlowId()));
                }
                flowRule.setToIdName(flowRuleMapper.selectUserNameByUserId(flowRule.getToId()));
            }
            json.setTotleNum(pageParams.getTotal());
            json.setObj(flowRules);
            json.setMsg("ok");
            json.setFlag(0);
        }catch(Exception e){
            e.printStackTrace();
            json.setMsg("err");
            json.setFlag(1);
        }

        return  json;
    }

    /**
     * 创建作者:   季佳伟
     * 创建日期:   2017/8/21 17:10
     * 方法介绍:   添加一条工作委托
     * 参数说明:
     * @return
     */
    @Override
    public ToJson<FlowRule> insertFlowRule(FlowRule flowRule, HttpServletRequest request){
        ToJson<FlowRule> json = new ToJson<FlowRule>();
        //根据flowId和userId查出记录
        FlowRule flowRuleDB=flowRuleMapper.selectFlowRuleByFlowIdAndUserId(flowRule.getFlowId(),flowRule.getUserId());
        //如果是空的，则代表数据库中没有此数据，可以插入。
        if(flowRuleDB==null){
            try{
                Cookie redisSessionId = CookiesUtil.getCookieByName(request, "redisSessionId");
                Users users= SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(),redisSessionId);
                Date date = new Date();
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
                String format = simpleDateFormat.format(date);
                //创建人
                Integer uid = users.getUid();
                flowRule.setCreateUser(uid.toString());
                //最后修改人
                flowRule.setUpdateUser(uid.toString());
                //创建时间
                flowRule.setCreateTime(format);
                //最后修改时间
                flowRule.setUpdateTime(format);
                int i = flowRuleMapper.insertSelective(flowRule);
                if (i>0){
                    json.setObject(flowRule);
                    json.setMsg("ok");
                    json.setFlag(0);
                }else {
                    json.setMsg("插入失败");
                    json.setFlag(1);
                }
            }catch (Exception e){
                e.printStackTrace();
                json.setMsg("error");
                json.setFlag(1);
            }
            //否则返回提示信息
        }else{
            json.setMsg("新建失败，此委托人再此流程中已存在被委托人");
            json.setFlag(1);
        }
        return  json;
    }
    /**
     * 创建作者:   季佳伟
     * 创建日期:   2017/8/21 17:20
     * 方法介绍:   修改工作委托的状态为开始=“1”
     * 参数说明:
     * @return
     */
    @Override
    public ToJson<FlowRule> updateStatusToStart(Integer[] ruleId){
        ToJson<FlowRule> json = new ToJson<FlowRule>();
        try{
            int a =0;
            if(ruleId==null||ruleId.length<1) {
                a = flowRuleMapper.updateStatusToStartAll();
            }else{
                a = flowRuleMapper.updateStatusToStart(ruleId);
            }
            if(a > 0){
                json.setFlag(0);
                json.setMsg("ok");

            }else{
                json.setFlag(1);
                json.setMsg("err");
            }

        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg("error");
        }

        return  json;
    }
    /**
     * 创建作者:   季佳伟
     * 创建日期:   2017/8/21 17:30
     * 方法介绍:   修改工作委托的状态为关闭=“0”
     * 参数说明:
     * @return
     */
    @Override
    public ToJson<FlowRule> updateStatusToEnd(Integer[] ruleId){
        ToJson<FlowRule> json = new ToJson<FlowRule>();
        try{
            int a=0;
            if(ruleId==null||ruleId.length<1) {
                a = flowRuleMapper.updateStatusToEndAll();
            }else{
                 a = flowRuleMapper.updateStatusToEnd(ruleId);
            }
            if(a > 0){
                json.setFlag(0);
                json.setMsg("ok");

            }else{
                json.setFlag(1);
                json.setMsg("err");
            }

        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg("error");
        }

        return  json;
    }



    /**
     * 创建作者:   季佳伟
     * 创建日期:   2017/8/21 17:20
     * 方法介绍:   删除工作委托
     * 参数说明:
     * @return
     */
    @Override
    public ToJson<FlowRule> deleteFlowRule(Integer[] ruleId){
        ToJson<FlowRule> json = new ToJson<FlowRule>();
        try{
            int a =0;
            if(ruleId==null || ruleId.length<1){
              a=  flowRuleMapper.deleteFlowRuleAll();
            }else{
                a = flowRuleMapper.deleteFlowRule(ruleId);
            }

            if(a > 0){
                json.setFlag(0);
                json.setMsg("ok");

            }else{
                json.setFlag(1);
                json.setMsg("err");
            }

        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg("error");
        }

        return json;
    }

    /**
     * 创建作者:   季佳伟
     * 创建日期:   2017/8/23 16:20
     * 方法介绍:   根据流程名称，委托人姓名，被委托人姓名查询工作委托
     * 参数说明:
     * @return
     */
    @Override
    public ToJson<FlowRule> selectFlowRule(Map maps){
        ToJson<FlowRule> json = new ToJson();
        try{
            List<FlowRule> flowRules = flowRuleMapper.selectFlowRule(maps);
            //循环添加名字
            for (FlowRule flowRule:flowRules){
                flowRule.setUserIdName(flowRuleMapper.selectUserNameByUserId(flowRule.getUserId()));
                flowRule.setFlowName(flowRuleMapper.selectFlowNameByFlowId(flowRule.getFlowId()));
                flowRule.setToIdName(flowRuleMapper.selectUserNameByUserId(flowRule.getToId()));
            }
            PageParams pageParams=(PageParams) maps.get("pageParams");
            json.setObj(flowRules);
            json.setTotleNum(pageParams.getTotal());
            json.setMsg("ok");
            json.setFlag(0);
        }catch(Exception e){
            e.printStackTrace();
            json.setMsg("err");
            json.setFlag(1);
        }

        return  json;
    }

    //显示编辑
    @Override
    public ToJson selectFlowRuleById(Integer ruleId) {
      ;ToJson<FlowRule> json=new ToJson();
        try{
            FlowRule flowRule = flowRuleMapper.selectFlowRuleById(ruleId);
            //添加名字
            flowRule.setFlowName(flowRuleMapper.selectFlowNameByFlowId(flowRule.getFlowId()));
            flowRule.setUserIdName(flowRuleMapper.selectUserNameByUserId(flowRule.getUserId()));
            flowRule.setToIdName(flowRuleMapper.selectUserNameByUserId(flowRule.getToId()));
            json.setObject(flowRule);
            json.setMsg("ok");
            json.setFlag(0);
        }catch(Exception e){
            e.printStackTrace();
            json.setMsg("err");
            json.setFlag(1);
        }

        return json;
    }
/*
    @Override
    public ToJson updateStatus(Integer flowId, Integer status) {
        ToJson<FlowRule> json = new ToJson<FlowRule>();
        try{
            int a = flowRuleMapper.updateStatus(flowId,status);
            if(a > 0){
                json.setFlag(0);
                json.setMsg("ok");

            }else{
                json.setFlag(1);
                json.setMsg("err");
            }

        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg("error");
        }

        return  json;
    }*/


    /**
    * @创建作者:李然  Lr
    * @方法描述：修改
    * @创建时间：18:33 2018/7/24

    **/
    @Override
    public ToJson updateFlowRule(FlowRule flowRule,HttpServletRequest request) {
        ToJson<FlowRule> json = new ToJson<FlowRule>();
        try{
            Cookie redisSessionId = CookiesUtil.getCookieByName(request, "redisSessionId");
            Users users= SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(),redisSessionId);
            Date date = new Date();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
            String format = simpleDateFormat.format(date);
            flowRule.setUpdateUser(users.getUid().toString());
            flowRule.setUpdateTime(format);
            int a = flowRuleMapper.updateByPrimaryKeySelective(flowRule);
            if(a > 0){
                json.setFlag(0);
                json.setMsg("ok");

            }else{
                json.setFlag(1);
                json.setMsg("err");
            }

        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg("error");
        }

        return  json;
    }
}
